package com.onion.mvc.utils;

import com.onion.utils.support.exception.OnionException;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class WebRequestUtils {

    /**
     * 获取ServletRequest Body数据
     * @param request
     * @return
     */
    public static String getBodyData(HttpServletRequest request){
        try {
            if(!request.getContentType().startsWith("application/json")){
                throw new OnionException(-1, "无效的ContentType:"+request.getContentType());
            }
            byte[] byteBuffer = new byte[request.getContentLength()];
            IOUtils.readFully(request.getInputStream(), byteBuffer);
            return new String(byteBuffer, "UTF-8");
        } catch (IOException e) {
            throw new OnionException(-1, "IOException:" + e.getMessage());
        }
    }

    public static String getTokenByHeader(HttpServletRequest httpRequest){
        String xToken = httpRequest.getHeader("");

        return xToken;
    }

}